#!/bin/bash
# release
#
#{{IS_NOTE
#	Purpose:
#		
#	Description:
#		
#	History:
#		Mon Nov 14 14:29:09     2005, Created by tomyeh
#}}IS_NOTE
#
#Copyright (C) 2005 Potix Corporation. All Rights Reserved.
#
#{{IS_RIGHT
#}}IS_RIGHT
#
stamp=$(date +%Y-%m-%d)

target=$1

setting=build.setting.local
if [ ! -f $setting ] ; then
	setting=build.setting
	if [ ! -f $setting ] ; then
		echo $setting must be specified
		exit 1
	fi
fi
javadocdir=$(grep '^javadoc' $setting)
javadocdir=${javadocdir#javadoc=}
if [ "$javadocdir" = "" ] ; then
	echo javadoc must be specified in $setting
	exit 1
fi
javadocdir=${javadocdir/\~/$HOME}
if [ ! -d "$javadocdir" ] ; then
	echo $javadocdir is not found
	exit 1
fi
javadocdir=${javadocdir/\~/$HOME}
if [ ! -d "$javadocdir/jsdoc" ] ; then
	echo $javadocdir/jsdoc is not found
	exit 1
fi

#if [ "$target" == "official" ] ; then
#	htmldocdir=$(grep '^htmldoc' $setting)
#	htmldocdir=${htmldocdir#htmldoc=}
#	if [ "$htmldocdir" = "" ] ; then
#		echo htmldoc must be specified in $setting
#		exit 1
#	fi
#	htmldocdir=${htmldocdir/\~/$HOME}
#	if [ ! -d "$htmldocdir/devguide" ] ; then
#		echo $htmldocdir/devguide is not found
#		exit 1
#	fi
#fi

ver=$(grep '^version=' $setting)
ver=${ver#version=}

#jar
if [ "$target" == "official" ] ; then
	binnm=zel-bin-$ver
	javadocnm=zel-javadoc-$ver
	mvnjavadocnm=zel-$ver-javadoc
	srcnm=zel-src-$ver
	echo "Release $binnm..."
elif [ "$target" == "" ] || [ "$target" == "daily" ] ; then
	target=daily

	bindnm=zel-bin-$ver-$stamp
	javadocnm=zel-javadoc-$ver-$stamp
	mvnjavadocnm=zel-$ver-$stamp-javadoc
	srcnm=zel-src-$ver-$stamp
	docnm=zel-doc-$ver-$stamp
	echo "Release $binnm..."
else
	echo "Usage: release [ official | daily ]"
	echo Default: daily
	exit 1
fi

rm -rf /tmp/zel
maindir="$(pwd)"
echo "Working directory $maindir"

#----
#Prepare the source codes
#----
if [ "$target" == "official" ] || [ "$target" == "daily" ] ; then
	mkdir -p /tmp/zel/$srcnm/bin
	mkdir -p /tmp/zel/$srcnm/dist/src
	mkdir -p /tmp/zel/$srcnm/doc
	chmod -R 755 $maindir
	echo Prepare ${srcnm}.zip
	
	cd $maindir
	if [ "$(pwd)" != "$maindir" ] ; then
		echo fail to change to directory $maindir
		exit 1
	fi
		
	cp bin/* /tmp/zel/$srcnm/bin
	cp build* /tmp/zel/$srcnm
	cp -rp bin /tmp/zel/$srcnm
	cp -rp zel /tmp/zel/$srcnm
	
	cd /tmp/zel/$srcnm
	if [ "$(pwd)" != "/tmp/zel/$srcnm" ] ; then
		echo fail to change to directory /tmp/zel/$srcnm
		exit 1
	fi
	rm -rf zel/codegen
	rm -rf zel/debug
	rm -rf zel/target
	
	find -L -name .svn | xargs rm -rf
	find -name .project | xargs rm -rf
	find -name .classpath | xargs rm -rf
	find -name .settings | xargs rm -rf	
	
	cd /tmp/zel
	if [ "$(pwd)" != "/tmp/zel" ] ; then
		echo fail to change to directory /tmp/zel
		exit 1
	fi
	
	#prepare *-sources.jar
	echo Prepare '*-sources.jar'
	mkdir -p src/zel
	predir="$(pwd)"
	cd $srcnm
	if [ "$(pwd)" == "$predir" ] ; then
		echo fail to change to directory $srcnm
		exit 1
	fi
	
	cp -rp zel/src/* ../src/zel
	
	cd ..
	
	rm -rf src/*/archive
	
	echo Output '*-sources.jar'
	cd src

	find -L -name 'package.html' | xargs rm -rf
	echo jar zel-sources.jar
	jar cfM zel-sources.jar -C zel .
	
	chmod 644 *.jar
	mv *-sources.jar ..
	cd ..
	rm -rf src

	cd $maindir
	
	mv -f /tmp/zel/*-sources.jar /tmp/zel/$srcnm/dist/src
	cd /tmp/zel
	echo Output ${srcnm}.zip
	zip -qr9 ${srcnm}.zip $srcnm
	
	#----
	#Prepare the javadoc
	#----
	if [ "$target" == "official" ] ; then
		#Prepare the doc
		cd $maindir
		$maindir/bin/javadoc "$javadocdir/zel"
	fi

	#Prepare the java doc
	cd $javadocdir

	echo Output ${mvnjavadocnm}.jar
	jar cfM ${mvnjavadocnm}.jar -C zel .
	mv ${mvnjavadocnm}.jar /tmp/zel
	
	#----
	#Prepare the binary release
	#----
	mkdir -p /tmp/zel/$binnm/dist/lib/ext
	mkdir -p /tmp/zel/$binnm/doc

	cd /tmp/zel

	if [ "$target" == "official" ] ; then
		#Prepare maven repository
		function mvnrepo {
			local vertamp=`echo $ver | sed 's/-/./'`
			mkdir -p $1
			if [ $2 ] ; then
				cp /tmp/zel/$srcnm/dist/src/$1-sources.jar $1/$1-$vertamp-sources.jar
			fi
			
			sed -e "s/{version}/$vertamp/" $maindir/$1/pom.xml > $1/pom.xml
			cp $maindir/dist/lib/$1.jar $1/$1-$vertamp.jar
			
			mv /tmp/zel/${mvnjavadocnm}.jar $1
			
			jar cfM $1-$vertamp-bundle.jar -C $1 .
		}
		mkdir -p maventmp
		mkdir -p /tmp/zel/maven
		cd maventmp
		mvnrepo zel true
		mv *-bundle.jar /tmp/zel/maven
		cd ..
		rm -rf maventmp
	else
		#Prepare maven repository
		function mvnrepo {
			local vertamp=`echo $ver | sed 's/-/./'`.$(date +%Y%m%d)
			mkdir -p $1
			if [ $2 ] ; then
				cp /tmp/zel/$srcnm/dist/src/$1-sources.jar $1/$1-$vertamp-sources.jar
			fi
			
			sed -e "s/{version}/$vertamp/" $maindir/$1/pom.xml > $1/pom.xml
			cp $maindir/dist/lib/$1.jar $1/$1-$vertamp.jar
			jar cfM $1-$vertamp-bundle.jar -C $1 .
		}
		mkdir -p maventmp
		mkdir -p /tmp/zel/maven
		cd maventmp
		mvnrepo zel true
		mv *-bundle.jar /tmp/zel/maven
		cd ..
		rm -rf maventmp
	fi

	mkdir -p /tmp/zel/$binnm/dist/src
	cp /tmp/zel/$srcnm/dist/src/* /tmp/zel/$binnm/dist/src/
	
	#Ouput ${binnm}
	if [ "$target" == "official" ] ; then
		cd /tmp/zel/$srcnm/dist/src

		cd /tmp/zel
		echo Output ${binnm}.zip
		zip -qr9 ${binnm}.zip $binnm
	fi
	
	cd /tmp/zel
	
	rm -rf $binnm
	
	#Prepare the java doc
	cd $javadocdir

	echo Output ${javadocnm}.zip
	zip -qr9 ${javadocnm}.zip zel
	mv ${javadocnm}.zip /tmp/zel

	cd /tmp/zel
	if [ "$(pwd)" != "/tmp/zel" ] ; then
		echo fail to change to directory /tmp/zel
		exit 1
	fi
	find -L -name .svn | xargs rm -rf
	find -name .project | xargs rm -rf
	find -name .classpath | xargs rm -rf
	find -name .settings | xargs rm -rf

	rm -rf $srcnm
fi
